package com.chpro.common;

import java.math.BigDecimal;

/**
 * 金额工具类
 * Created by Administrator on 14-8-30.
 */
public class MoneyUtil {

    /**
     * 元转换为分
     * @param dealvalue
     * @return
     */
    public static String convertyuantofen(String dealvalue) {
        if (dealvalue == null) {
            return "";
        }
        String temp = dealvalue.trim();
        int postion = -100;
        postion = temp.indexOf(".");
        if (postion != 0) {
            BigDecimal inhead = new BigDecimal(0);
            String inheads = "";
            String infoots = "";
            if (postion != -1) {
                BigDecimal head = new BigDecimal(temp.substring(0, postion));
                String foots = temp.substring(postion + 1, temp.length());
                if (foots.length() >= 3) {
                    inhead = new BigDecimal(foots.substring(0, 2));
                    infoots = foots.substring(2, foots.length());
                    temp = (head.multiply(new BigDecimal(100)).add(inhead)).toString();
                    return temp;
                } else if (foots.length() == 2) {
                    temp = (head.multiply(new BigDecimal(100)).add(new BigDecimal(foots))).toString();
                    return temp;
                } else {
                    temp = (head.multiply(new BigDecimal(100)).add(new BigDecimal(foots).multiply(new BigDecimal(10)))).toString();
                    return temp;
                }

            } else {
                BigDecimal head = new BigDecimal(temp);
                temp = head.multiply(new BigDecimal(100)).toString();
                return temp;
            }
        } else {
            BigDecimal head = new BigDecimal(temp);
            temp = head.multiply(new BigDecimal(100)).toString();
            return temp;
        }
    }

    /**
     * 分转换为元
     * @param dealvalue
     * @return
     */
    public static String convertfentoyuan(String dealvalue) {

        if (dealvalue == null || dealvalue.trim().equals(""))
            return "";

        if (dealvalue.length() > 9) {
            String largeyuan = dealvalue.substring(0, dealvalue.length() - 2)
                    + "." + dealvalue.substring(dealvalue.length() - 2);
            return largeyuan;
        }
        int valueconvert = Integer.parseInt(dealvalue);
        double dtemp = (double) valueconvert / 100;
        String yuan = Double.toString(dtemp);
        int position = 0;
        if ((position = yuan.indexOf(".")) != -1) {
            if (yuan.substring(position + 1).length() == 1) {
                yuan = yuan + "0";
            }
        }
        return yuan;
    }
}
